package com.hisan.freeride.common.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;

import com.hisan.freeride.common.dao.CityManger;
import com.hisan.freeride.common.utils.FontsOverride;
import com.karumi.dexter.Dexter;
import com.king.thread.nevercrash.NeverCrash;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.wonderkiln.blurkit.BlurKit;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * 创建时间 : 2017/6/2
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：app启动基类
 */
public class BaseAppLication  extends Application{
    private static BaseAppLication instance;
    //是否已登入
    private static boolean islogin=false;
    //城市id
    private int cityid=0;
    //推荐码
    private String  recommend="";
    //防止预加载
    private  boolean ispage=true;
    //阿里
    private CloudPushService pushService;
    //获取定位街道
    private String address;
    //判断实名注册认证是否成功
    private int is_realname=0;

    public CityManger getCityManger() {
        return cityManger;
    }

    private CityManger cityManger;
    public int getIs_realname() {
        return is_realname;
    }

    public void setIs_realname(int is_realname) {
        this.is_realname = is_realname;
    }


    public CloudPushService getPushService() {
        return pushService;
    }

    public void setPushService(CloudPushService pushService) {
        this.pushService = pushService;
    }
    /**
     * 实例化Application，单例模式
     *
     * @return 返回单例的Application对象
     */
    public synchronized static BaseAppLication getInstance() {
        if (instance == null)
            instance = new BaseAppLication();
        return instance;
    }

    public  boolean islogin() {
        return islogin;
    }

    public  void setIslogin(boolean islogin) {
       this.islogin = islogin;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        //图片模糊
        BlurKit.init(instance);
        NeverCrash.init((t, e) -> LogUtils.d("xxx", "系统异常："+e.getMessage()));
      //  Stetho.initializeWithDefaults(instance);
        initCloudChannel(this);
        cityManger=new CityManger(this);
        //初始化6.0动态库
        Dexter.initialize(instance);
        //app管理工具类
        Utils.init(instance);

        //初始化图片加载工具类
        Fresco.initialize(instance);
        FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/handmadeTyoperiter.ttf");
        initOKGO();
    }

    //初始化网络请求工具
    private void initOKGO(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //log相关
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("xxx");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setColorLevel(Level.INFO);                               //log颜色级别，决定了log在控制台显示的颜色
        builder.addInterceptor(loggingInterceptor);
        //使用数据库保持cookie，如果cookie不过期，则一直有效
        // 添加OkGo默认debug日志
        //超时时间设置，默认60秒
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);      //全局的读取超时时间
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);     //全局的写入超时时间
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);   //全局的连接超时时间
        //https相关设置
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
        OkGo.getInstance().init(BaseAppLication.instance)                           //必须调用初始化
                .setOkHttpClient(builder.build())               //建议设置OkHttpClient，不设置会使用默认的
                .setRetryCount(3);//全局统一超时重连次数
    }
    /**
     * 重启APP
     */
    public void restartApplication() {
        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    public int getCityid() {
        return cityid;
    }
     public void setCityid(int cityid) {
        this.cityid = cityid;
    }


    public String getRecommend() {
        return recommend;
    }

    public void setRecommend(String recommend) {
        this.recommend = recommend;
    }
    public boolean ispage() {
        return ispage;
    }

    public void setIspage(boolean ispage) {
        this.ispage = ispage;
    }


    public String getAddress() {
        return address;
    }

    public void setAddress(String street) {
        this.address = street;
    }

    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
         pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                LogUtils.v("xxx",response);
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                LogUtils.v("xxx",errorCode+" : "+errorMessage);
            }
        });
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
